var core = {};

core.getBaseUrl = function() {
  var exp = new RegExp(/http:\/\/([a-z0-9.:-]+)/);
  var match = window.location.href.match(exp);
  if (match) {
    return ['http://', match[1], '/'].join('');
  }

  return '';
};

core.urlEscape = function(url) {
  url = url.replace(/ /g, '+');
  return url;
}

core.bind = function(functionName, target) {
  return function() {
    if (functionName.apply) {
      return functionName.apply(target, arguments);
    }
  };
};

core.getElementById = function(id, clear) {
  var element = document.getElementById(id);

  if (element && clear === true) {
    element.innerHTML = '';
  }

  return element;
};

core.getElementsByTagName = function(tagName) {
  return document.getElementsByTagName(tagName);
};

core.createElement = function(name, innerText, properties, styleProperties) {
  var element = document.createElement(name);

  for (x in properties) {
    element[x] = properties[x];
  }

  for (x in styleProperties) {
    element.style[x] = styleProperties[x];
  }

  if (innerText && innerText != '') {
    element.innerHTML = innerText;
  }

  return element;
};

core.appendNode = function(element, target) {
  if (target) {
    var type = typeof target;

    if (type == 'string') {
      var targetElmt = document.getElementById(target);
      targetElmt.appendChild(element);
    } else {
      target.appendChild(element);
    }
  }
};

core.AjaxRequest = function(method, url, onLoad, onError) {
  this.transport = this.getTransport();

  this.onError = onError;
  this.onLoad = onLoad;
  this.method = method;
  this.url = url;
};

core.AjaxRequest.prototype.send = function(asynchronous) {
  if (asynchronous == null) {
    asynchronous = true;
  }

  if (this.transport) {
    try {
      var requestObjt = this;
      this.transport.onreadystatechange = function() {
        requestObjt.onReadyState.call(requestObjt);
      }

      this.transport.open(this.method, this.url, asynchronous);
      this.transport.send(this.url);
    }
    catch (e) {
      if (this.onError) {
        this.onError(e);
      }
    }
  }
};

core.AjaxRequest.prototype.onReadyState = function() {
  if (this.transport.readyState == 4) {
    if (this.transport.status==200 || this.transport.status==0) {
      if (this.onLoad) {
        this.onLoad(this.transport.responseText, this.transport.responseXML);
      }
    } else {
      if (this.onError) {
        this.onError();
      }
    }
  }
};

core.AjaxRequest.prototype.getTransport = function() {
  if (window.XMLHttpRequest) {
    return new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    return new ActiveXObject('Microsoft.XMLHTTP');
  }

  return null;
};